package com.hualan.sfzyproject.controller;

import com.hualan.sfzyproject.domain.FeaturedArticle;
import com.hualan.sfzyproject.service.FeaturedArticleService;
import com.hualan.sfzyproject.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/featuredArticle")
public class FeaturedArticleController {
    @Autowired
    private FeaturedArticleService featuredArticleService;

    @PostMapping
    public R insert(@RequestBody FeaturedArticle featuredArticle) {
        int result = featuredArticleService.insert(featuredArticle);
        if (result > 0) {
            return R.ok(result);
        }
        return R.error("插入失败");
    }

    @PutMapping
    public R update(@RequestBody FeaturedArticle featuredArticle) {
        int result = featuredArticleService.update(featuredArticle);
        if (result > 0) {
            return R.ok(result);
        }
        return R.error("更新失败");
    }

    @DeleteMapping
    public R delete(Long id) {
        System.out.println("==========================================");
        System.out.println("id为"+id);
        int result = featuredArticleService.delete(id);
        if (result > 0) {
            return R.ok(result);
        }
        return R.error("删除失败");
    }

    @GetMapping("/{id}")
    public R selectById(@PathVariable Long id) {
        FeaturedArticle featuredArticle = featuredArticleService.selectById(id);
        if (featuredArticle != null) {
            return R.ok(featuredArticle);
        }
        return R.error("查询失败");
    }

    @GetMapping("/selectByConsultantId/{consultantId}")
    public R selectByConsultantId(@PathVariable int consultantId) {
        List<FeaturedArticle> featuredArticle = featuredArticleService.selectByConsultantId(consultantId);
        if (featuredArticle != null) {
            return R.ok(featuredArticle);
        }
        return R.error("查询失败");
    }

    @GetMapping("/all")
    public R selectAll(@RequestParam(defaultValue = "1") int pageNum,
                       @RequestParam(defaultValue = "5") int pageSize) {
        List<FeaturedArticle> featuredArticle = featuredArticleService.selectAll(pageNum, pageSize);
        if (featuredArticle != null) {
            return R.ok(featuredArticle);
        }
        return R.error("查询失败");
    }
}
